package org.example.model;

/**
 * @author by PengHongChao
 * @date 2025/6/23 15:40
 */

import lombok.Data;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 数据库同步配置实体
 */
/**
 * 数据库同步配置实体
 */
@Data
public class SyncConfig {
    private Long id;

    private Long sourceDataSourceId; // 源数据源ID
    private Long targetDataSourceId; // 目标数据源ID

    @NotBlank(message = "源表不能为空")
    @Size(max = 100, message = "源表名称长度不能超过100")
    private String sourceTable;

    @NotBlank(message = "目标表不能为空")
    @Size(max = 100, message = "目标表名称长度不能超过100")
    private String targetTable;

    @NotBlank(message = "同步列不能为空")
    @Size(max = 50, message = "同步列长度不能超过50")
    private String syncColumn;

    @NotBlank(message = "唯一键不能为空")
    @Size(max = 200, message = "唯一键长度不能超过200")
    private String uniqueKey;

    @Size(max = 50, message = "删除标记列长度不能超过50")
    private String deletedColumn;

    @NotNull(message = "同步间隔不能为空")
    @Min(value = 1, message = "同步间隔最小为1分钟")
    private Integer syncInterval = 5;

    private LocalDateTime lastSyncTime;
    private LocalDateTime lastSuccessTime;
    private Integer errorCount = 0;

    @NotNull(message = "启用状态不能为空")
    private Boolean enabled = true;

    @Size(max = 100, message = "执行窗口长度不能超过100")
    private String executionWindow = "ALL";

    @NotBlank(message = "调度类型不能为空 // FIXED | CRON")
    private String scheduleType = "FIXED";

    @Size(max = 50, message = "Cron表达式长度不能超过50")
    private String cronExpression;

    private Integer priority = 2;
    private LocalDateTime createdTime = LocalDateTime.now();
    private LocalDateTime updatedTime = LocalDateTime.now();

    /**
     * 获取有效优先级
     * @return 1:高, 2:中, 3:低
     */
    public int getEffectivePriority() {
        if (syncInterval <= 3) return 1;
        if (syncInterval <= 15) return 2;
        return 3;
    }
}
